package cn.duwey.scs.service.user.api.impl;

import cn.duwey.scs.api.user.UserApi;
import cn.duwey.scs.api.user.dto.UserDTO;
import cn.duwey.scs.common.dto.PageResult;
import cn.duwey.scs.common.dto.ServiceResult;
import cn.duwey.scs.common.entity.UserEntity;
import cn.duwey.scs.common.support.RpcSupport;
import cn.duwey.scs.common.util.BeanUtils;
import cn.duwey.scs.service.user.service.IUserService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class UserApiImpl implements UserApi {

    @Autowired
    IUserService userService;

    @Override
    public ServiceResult<UserDTO> createUser(@RequestBody UserDTO userDTO) {
        UserEntity userEntity = new UserEntity();

        BeanUtils.copyProperties(userDTO, userEntity);

        userEntity = userService.save(userEntity);

        BeanUtils.copyProperties(userEntity, userDTO);

        return RpcSupport.wrapSuccess(userDTO);
    }

    @Override
    public ServiceResult<UserDTO> updateById(@RequestBody UserDTO userDTO) {
        return null;
    }

    @Override
    public ServiceResult<UserDTO> findById(@PathVariable("id") Long id) {

        List<UserEntity> list = new ArrayList<>();

        for (int i = 1; i < 10; i++) {
            UserEntity entity = new UserEntity();
            entity.setUsername("username" + i);
            entity.setRealname("realname" + i);
            entity.setNickname("nickname" + i);
            entity.setAge(i);
            try {
                entity.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-02"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            entity.setEmail("email" + i);

            list.add(entity);
        }

        userService.saveAll(list);

        return null;
    }

    @Override
    public ServiceResult<UserDTO> findByUsername(@RequestParam("username") String username) {
        return null;
    }

    @Override
    public ServiceResult<PageResult<UserDTO>> findPage(
            @RequestParam("currentPage") Integer currentPage,
            @RequestParam("pageSize") Integer pageSize) {
        return null;
    }
}
